package cn.lbd.income.controller;

import cn.lbd.income.dao.IncomeDao;
import cn.lbd.income.entity.Income;
import cn.lbd.income.service.CityMicService;
import cn.lbd.income.utils.HttpResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.lbd.income.utils.JasperReportUtils.*;


@RestController
@CrossOrigin(origins = "*")
@Slf4j
@RequestMapping("/reports")
//@PreAuthorize("hasAuthority('11100125')")  //10000025无此权限
public class IncomeController {
    @Autowired(required = false)
    private IncomeDao incomeDao;
    @Autowired
    private CityMicService cityService;

    /**
      分页插件封装后的json，前端基于res.data前缀来取相应数据。res.data.list => income分页列表数据
     */
    @RequestMapping(value = "/incomeList/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public HttpResult listData(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        log.info("报表微服务调用....");
        PageHelper.startPage(pageNum, pageSize);
        List<Income> incomes = incomeDao.selectByExample(null);
        //log.info("Query List......" + incomes);
        PageInfo<Income> pageInfo = new PageInfo<Income>(incomes, pageSize);
        System.out.println(cityService.test().getMsg());
        return HttpResult.ok(pageInfo);
    }

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public HttpResult test() {
        log.info("报表微服务调用....");
        return HttpResult.ok("Report Mic Service >...................");
    }

    @RequestMapping("income")
    public void income(@RequestParam("type") String type, HttpServletResponse response) {
        try {
            List<Income> incomeList = incomeDao.selectByExample(null);
            Map<String, Object> parameters = new HashMap();
            String[] split = convertDate().split("-");
            parameters.put("year", split[0]);
            parameters.put("month", split[1]);
            parameters.put("day", split[2]);
            parameters.put("makeUnit", "xxx责任有限公司");
            parameters.put("maker", "nlboss");
            String fileName = getJasperFileDir("income");
            if ("pdf".equals(type)) {
                exportToPdf(fileName, parameters, incomeList, response);
            } else if ("html".equals(type)) {
                exportToHtml(fileName, parameters, incomeList, response);
            } else if ("xlsx".equals(type)) {
                exportToExcel(fileName, parameters, incomeList, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @RequestMapping("income1")
    public void income1(@RequestParam("type") String type, HttpServletResponse response) {
        try {
            List<Income> incomeList = incomeDao.selectByExample(null);
            Map<String, Object> parameters = new HashMap();
            String[] split = convertDate().split("-");
            parameters.put("year", split[0]);
            parameters.put("month", split[1]);
            parameters.put("day", split[2]);
            parameters.put("makeUnit", "xxx责任有限公司");
            parameters.put("maker", "nlboss");
            String fileName = getJasperFileDir("income1");
            if ("pdf".equals(type)) {
                exportToPdf(fileName, parameters, incomeList, response);
            } else if ("html".equals(type)) {
                exportToHtml(fileName, parameters, incomeList, response);
            } else if ("xlsx".equals(type)) {
                exportToExcel(fileName, parameters, incomeList, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}